﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NoteSystem.utils;
using System.Data;
namespace NoteSystem
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DataSet ds = DbHelper.Query("select*from citys");
            ddlCity.DataSource = ds;
            ddlCity.DataTextField = "city_name";
            ddlCity.DataValueField = "id";
            ddlCity.DataBind();
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            #region 注册
            //1.获取用户输入数据
            string account = txtAccount.Text.Trim();
            string birthday = txtBirthday.Text.Trim();
            string pwd = txtPwd.Text.Trim();
            int city_id = Convert.ToInt32(ddlCity.SelectedValue);
            string img_url = img.ImageUrl;//图片组件路径
            int gender = Convert.ToInt32(rbtGender.SelectedValue);
            string phone = txtPhone.Text.Trim();
            string email = txtEmail.Text.Trim();
            string qq = txtQQ.Text.Trim();
            int flag = DbHelper.ExecuteSql($@"insert into users(account,birthday,pwd,city_id,img_url,phone,email,qq,gender,login_count,last_login_time,manager,type) 
            values('{account}','{birthday}', '{pwd}',{city_id},'{img_url}','{phone}','{email}',{qq},{gender},0,'{DateTime.Now}',0,0)");
            if (flag > 0)
            {
                Response.Write("<script>alert('注册成功，请等待管理员审批');window.location.href='Login.aspx'</script>");
            }
            #endregion
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            #region 图片上传
            string useName = ".jpg.png.gif";
            string name = fupImg.FileName;
            string exName = name.Substring(name.LastIndexOf('.'));//.png
            if (useName.Contains(exName) == false)
            {
                Response.Write("<script>alert('不支持该文件格式!')</script>");
                return;
            }
            if (fupImg.FileContent.Length > 1 * 1024 * 1024)
            {
                Response.Write("<script>alert('不能上传超过1M的图片!')</script>");
                return;
            }
            string path = Server.MapPath("images") + "\\" + name;
            if (name != "")
            {
                fupImg.SaveAs(path);//文件保存
            }
            img.ImageUrl = "images/" + name;
            #endregion
        }
    }
}